package com.tjetc.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tjetc.common.Result;
import com.tjetc.domain.Department;
import com.tjetc.domain.StaffsCount;
import com.tjetc.domain.News;
import com.tjetc.service.DepartmentService;
import com.tjetc.service.impl.DepartmentServiceImpl;
import com.tjetc.utils.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet("/department/*")
@MultipartConfig
public class DepartmentServlet extends HttpServlet {
    private DepartmentService departmentService = new DepartmentServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String methodName = uri.substring(uri.lastIndexOf("/") + 1);
        try {
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String curPage = request.getParameter("curPage");
        String ps = request.getParameter("pageSize");
        name=name==null?"":name;
        //接受请求参数
        //空处理:赋值给当前页码
        int currentPage = curPage == null ? 1:Integer.valueOf(curPage);
        //每页显示的条数
        int pageSize = ps == null ? 5 : Integer.parseInt(ps);
        //总记录数
        int count = departmentService.getCount(name);
        PageInfo<Department> pageInfo = new PageInfo<>(currentPage, count, pageSize);
        List<Department> list = departmentService.list(name,pageInfo);
        pageInfo.setList(list);
        System.out.println("科室信息 = " + list);
        new ObjectMapper().writeValue(response.getOutputStream(),pageInfo);
    }

    protected void listParentZero(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Department> departments = departmentService.queryPidZero();
        System.out.println(departments);
        new ObjectMapper().writeValue(response.getOutputStream(),departments);
    }
    protected void listParentDZero(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Department> departments = departmentService.queryPidDZero();
        new ObjectMapper().writeValue(response.getOutputStream(),departments);
    }

    protected void listStaffsData(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<StaffsCount> staffsData = departmentService.getStaffsData();
        new ObjectMapper().writeValue(response.getOutputStream(),staffsData);
    }

    protected void listParentNotZero(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Department> departments = departmentService.queryPidNotZero();
        new ObjectMapper().writeValue(response.getOutputStream(),departments);
    }

    protected void listAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Department> departments = departmentService.queryAll();
        new ObjectMapper().writeValue(response.getOutputStream(),departments);
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        Integer did = Integer.valueOf(request.getParameter("did"));
        String remarks = request.getParameter("remarks");
        Integer parent_id = Integer.valueOf(request.getParameter("parent_id"));
        Department department = new Department();
        department.setName(name);
        department.setDid(did);
        department.setRemarks(remarks);
        department.setStaffs(0);
        department.setBeds(0);
        department.setParent_id(parent_id);
        boolean b = departmentService.add(department);
        if (b){
            new ObjectMapper().writeValue(response.getOutputStream(), Result.success("添加成功"));
        }
        new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("添加失败，出异常喽！"));
    }

    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        String name = request.getParameter("name");
        Integer did = Integer.valueOf(request.getParameter("did"));
        String remarks = request.getParameter("remarks");
        Integer parent_id = Integer.valueOf(request.getParameter("parent_id"));
        Department department = new Department();
        department.setId(id);
        department.setName(name);
        department.setDid(did);
        department.setRemarks(remarks);
        department.setParent_id(parent_id);
        boolean b = departmentService.update(department);
        if (b){
            new ObjectMapper().writeValue(response.getOutputStream(), Result.success("修改成功"));
        }
        new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("修改失败，出异常喽！"));
    }

    protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        boolean b = departmentService.del(id);
        if (b){
            new ObjectMapper().writeValue(response.getOutputStream(), Result.success("删除成功"));
        }
        new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("删除失败"));
    }


    protected void listFindByParent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pid = Integer.valueOf(req.getParameter("parent_id"));
        List<Department> departments=departmentService.findByPid(pid);
        new ObjectMapper().writeValue(resp.getOutputStream(),departments);
    }
}
